<?php
class Custom_Utility_Parse
{
    public static function cvsToArray($file, $columnheadings = false,
            $delimiter = ';',$enclosure = "\"")
    {
        $row = 1;
        $rows = array();
        $handle = fopen($file, 'r');

        while (false !== ($data = fgetcsv($handle, 1000, $delimiter, $enclosure))) {
            if (!($columnheadings == false) && ($row == 1)) {
                $headingTexts = $data;
            } elseif (!($columnheadings == false)) {
                foreach ($data as $key => $value) {
                    unset($data[$key]);
                    $data[$headingTexts[$key]] = $value;
                }
                $rows[] = $data;
            } else {
                $rows[] = $data;
            }
            $row++;
        }

        fclose($handle);
        return $rows;
    }

    public static function arrayToObject($array)
    {
        if(!is_array($array)) {
            return $array;
        }

        $object = new stdClass();

        if (is_array($array) && count($array) > 0) {
            foreach ($array as $name=>$value) {
                $object->$name = self::arrayToObject($value);
            }
            return $object;
        }
        else {
            return FALSE;
        }
    }

    public static function stringToArray($string)
    {
        $option = array();

        $exp = explode(' ', $string);
        foreach ($exp as $key=>$value) {
            $arr = explode('=', $value);
            $option[$arr[0]] = $arr[1];
        }
        
        return $option;
    }

    public static function urlToParameters($url)
    {
        $query = '';
        $route = 'default';
        $params = array();
        $options = array();

        if (!$url) {
            $url = '/';
        }

        if ($pos = strpos($url, '?')) {
            $query = substr($url, $pos + 1);
            $url = substr($url, 0, $pos);
        }

        if ($url[0] == '@') {
            $route = substr($url, 1);
        } else if (false !== strpos($url, '/')) {
            list($arrayMain['controller'], $arrayMain['action']) = explode('/', $url);
        }

        if ($query) {
            parse_str($query,  $options);
        }
        
        if ($arrayMain) {
            $params = array_merge($arrayMain, $options);
        } else {
            $params = $options;
        }

        return array($route, $params);        
    }
}